package com.more.offer.util;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author pinoc
 * @date 2023/6/20
 */
public class ImageUtil2 {
	/**
	 * 添加字体的属性设置
	 */
	private Font font = new Font("微软雅黑", Font.BOLD, 20);
	private Graphics2D g = null;
	private int fontsize = 0;
	private int x = 0;
	private int y = 0;

	private BufferedImage getBufferedImageFromLocal(String filePath) throws IOException {
		return Thumbnails.of(filePath)
				.scale(1)
				.asBufferedImage();
	}

	private BufferedImage getBufferedImageFromUrl(String filePath) throws IOException {
		//asBufferedImage() 返回BufferedImage
		return Thumbnails.of(new URL(filePath))
				.scale(1)
				.asBufferedImage();
	}

	public static BufferedImage convert2CircularImg(BufferedImage buffer) throws IOException {
		//透明底的图片
		BufferedImage bufferedImage = new BufferedImage(buffer.getWidth(), buffer.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
		Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, buffer.getWidth(), buffer.getHeight());
		Graphics2D g2 = bufferedImage.createGraphics();
		g2.setClip(shape);
		// 使用 setRenderingHint 设置抗锯齿
		g2.drawImage(buffer, 0, 0, null);
		// 设置颜色
		g2.setBackground(Color.green);
		g2.dispose();
		return bufferedImage;
	}

	/**
	 * 百分比缩放图片
	 * @param inputImage 图片流
	 * @param newWidth 宽度
	 * @param newHeight 高度
	 * @return BufferedImage
	 */
	public static BufferedImage scaleByPercentage(BufferedImage inputImage, int newWidth, int newHeight) {
		// 获取原始图像透明度类型
		int type = inputImage.getColorModel().getTransparency();
		int width = inputImage.getWidth();
		int height = inputImage.getHeight();
		// 开启抗锯齿
		RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		// 使用高质量压缩
		renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		BufferedImage img = new BufferedImage(newWidth, newHeight, type);
		Graphics2D graphics2d = img.createGraphics();
		graphics2d.setRenderingHints(renderingHints);
		graphics2d.drawImage(inputImage, 0, 0, newWidth, newHeight, 0, 0, width, height, null);
		graphics2d.dispose();
		return img;
	}

	/**
	 * 些图片到本地
	 * @param newImage 图片路径
	 * @param img 图片流
	 * @return bool
	 */
	public boolean writeImageLocal(String newImage, BufferedImage img) {
		boolean isok = false;
		if (newImage != null && img != null) {
			try {
				File outputfile = new File(newImage);
				isok = ImageIO.write(img, "png", outputfile);
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
		return isok;
	}

	/**
	 * 给图片添加文字
	 * @param bufferedImage 图片流
	 * @param content 内容
	 * @param x 横坐标
	 * @param y 纵坐标
	 * @return BufferedImage
	 */
	public BufferedImage addContentToImg(BufferedImage bufferedImage, Object content, int x, int y) {
		try {
			int w = bufferedImage.getWidth();
			int h = bufferedImage.getHeight();
			g = bufferedImage.createGraphics();
			g.setBackground(Color.WHITE);
			// 设置字体颜色
			g.setColor(Color.BLACK);
			if (this.font != null) {
				g.setFont(this.font);
			}
			// 验证输出位置的纵坐标和横坐标
//			if (x >= h || y >= w) {
//				this.x = h - this.fontsize + 2;
//				this.y = w;
//			} else {
//				this.x = x;
//				this.y = y;
//			}
			g.setFont(new Font("微软雅黑", Font.BOLD, 22));
			if (content != null) {
				g.drawString(content.toString(), x, y);
			}
			g.setBackground(Color.WHITE);
			g.dispose();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return bufferedImage;
	}

	/**
	 * 合成图片
	 * @param b 图片b
	 * @param d 图片d
	 * @param x x坐标
	 * @param y y坐标
	 * @param height 高度
	 * @param weight 宽度
	 * @return
	 */
	public BufferedImage mergeImg(BufferedImage b, BufferedImage d, int x, int y, int weight, int height) {
		try {
			g = d.createGraphics();
			g.drawImage(b, x, y, weight, height, null);
			g.dispose();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return d;
	}

	/**
	 *
	 * @param bufferedImage 图片流
	 * @param d 比例
	 * @return Integer
	 */
	public static Integer doubleToInt(BufferedImage bufferedImage, double d){
		return (new Double((d * bufferedImage.getHeight()))).intValue();
	}

	public static void main(String[] args) throws IOException {
		ImageUtil2 imageUtil = new ImageUtil2();
		String imagePath = "F:\\downloads\\imgBuild\\1.jpg";
		String qrCodeUrl = "F:\\downloads\\imgBuild\\2.jpg";
		String head = "F:\\downloads\\imgBuild\\head.png";
		String newHead = "F:\\downloads\\imgBuild\\newHead.png";
		String inviteText = "F:\\downloads\\imgBuild\\inviteText.png";
		String newQrCode = "F:\\downloads\\imgBuild\\newQrCode.jpg";
		String genePath2 = "F:\\downloads\\imgBuild\\newMergeImg2.jpg";
		String withContentImgPath = "F:\\downloads\\imgBuild\\withContentImgPath.jpg";

		BufferedImage qrCodeImg = imageUtil.getBufferedImageFromLocal(qrCodeUrl);
		qrCodeImg = ImageUtil2.convert2CircularImg(qrCodeImg);
		qrCodeImg = ImageUtil2.scaleByPercentage(qrCodeImg, 210, 210);
//		imageUtil.writeImageLocal(newQrCode, qrCodeImg);

		BufferedImage headImg = imageUtil.getBufferedImageFromUrl("https://t.zyebank.cn:1903/api/iop/api/iop-pcweb/iop-img/mgm_active/59c6_20230614165737.jpg");
		headImg = ImageUtil2.convert2CircularImg(headImg);
		headImg = ImageUtil2.scaleByPercentage(headImg, 120, 120);
//		imageUtil.writeImageLocal(newHead, headImg);

		BufferedImage backImg = imageUtil.getBufferedImageFromLocal(imagePath);
		BufferedImage inviteTextImg = imageUtil.getBufferedImageFromLocal(inviteText);
		inviteTextImg = ImageUtil2.scaleByPercentage(inviteTextImg, 300, 38);
//		BufferedImage withQrCodeImg = imageUtil.mergeImg(qrCodeImg, backImg, 520, 800, 210, 210);
		String genePath = "F:\\downloads\\imgBuild\\newMergeImg.jpg";
//		BufferedImage withHeadImg = imageUtil.mergeImg(headImg, withQrCodeImg, 20, 850, 120, 120);
//		BufferedImage withInviteTextImg = imageUtil.mergeImg(inviteTextImg, withHeadImg, 20, 980, 300, 38);
//		imageUtil.writeImageLocal(genePath, withInviteTextImg);

		BufferedImage withContentBuffer = imageUtil.addContentToImg(backImg, "会飞的派大星", 150, 930);
		imageUtil.writeImageLocal(withContentImgPath, withContentBuffer);

		Thumbnails.of(withContentImgPath)
				//按比例放大缩小  >0   cale(scaleWidth,scaleHeight)
				.scale(1f)
				//设置生成图片的长宽 与scale 不能同时使用
//				.size(1000, 2047)
				// rotate(角度),正数：顺时针负数：逆时针
				.rotate(0)
				//.watermark(，水印图，透明度)
				.watermark(new CustomPosition(520, 800), qrCodeImg, 1f)
				.watermark(new CustomPosition(20, 850), headImg, 1f)
				.watermark(new CustomPosition(20, 980), inviteTextImg, 1f)
				//保存为文件的格式设置
				.outputFormat("jpg")
				//是否允许覆盖已存在的文件
				.allowOverwrite(true)
				//输出的图片质量  0~1 之间,否则报错
				.outputQuality(0.5f)
				.toFile(withContentImgPath);

	}
}
